#include <stdio.h>
#include <stdlib.h>

// 定义循环链表节点结构体
typedef struct ListNode {
    int num;
    struct ListNode *next;
} ListNode;

// 创建循环链表
ListNode *createCircularList(int n) {
    ListNode *head = NULL;
    ListNode *prev = NULL;

    for (int i = 1; i <= n; i++) {
        ListNode *newNode = (ListNode *)malloc(sizeof(ListNode));
        if (newNode == NULL) {
            fprintf(stderr, "内存分配失败\n");
            exit(EXIT_FAILURE);
        }
        newNode->num = i;
        newNode->next = NULL;

        if (head == NULL) {
            head = newNode;
        } else {
            prev->next = newNode;
        }
        prev = newNode;
    }

    // 形成循环链表
    prev->next = head;

    return head;
}

// 解决约瑟夫问题并返回猴王编号
int josephusProblem(ListNode *head, int m) {
    ListNode *p = head;
    ListNode *prev = NULL;

    while (p->next != p) {
        // 报数到m - 1
        for (int i = 1; i < m; i++) {
            prev = p;
            p = p->next;
        }

        // 删除报数到m的节点
        prev->next = p->next;
        free(p);
        p = prev->next;
    }

    int winner = p->num;
    free(p); // 释放最后一个节点
    return winner;
}

int main() {
    int n, m;

    while (1) {
        scanf("%d %d", &n, &m);

        if (n == 0 && m == 0) {
            break;
        }

        ListNode *circularList = createCircularList(n);
        int king = josephusProblem(circularList, m);
        printf("%d\n", king);
    }

    return 0;
}